package org.example.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Auther:Liu
 * @Testname:MD5Util
 * @Date:2025/8/5 17:33
 */
public class MD5Util {
    public static String encryptMD5(String data) {
        try {
            // 创建MessageDigest对象，参数为算法名
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 计算md5函数
            byte[] digest = md.digest(data.getBytes());
            // 将得到的字节数组变成字符串返回
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                int num = b & 0xff;
                String str = Integer.toHexString(num);
                if (str.length() == 1) {
                    sb.append("0");
                }
                sb.append(str);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}
